package cn.knight.org.controller;

import cn.knight.basic.annotation.PreAuthorize;
import cn.knight.basic.exception.BusinessException;
import cn.knight.basic.utils.ExcelUtil;
import cn.knight.org.domain.ShopAuditLog;
import cn.knight.org.service.IShopService;
import cn.knight.org.domain.Shop;
import cn.knight.org.query.ShopQuery;
import cn.knight.basic.utils.AjaxResult;
import cn.knight.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private  IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "店铺新增/修改", sn = "shop:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "店铺删除", sn = "shop:remove")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    /**
    * @Title: Enable
    * @Description: 激活接口，用户点击邮件中的激活链接跳转此接口
    * @Author: winnie
    * @Version: 1.0
    * @Date:  3/7/2022 上午9:35  
    * @Parameters: [id]
    * @Return void
    */
    @GetMapping("/Enable/{id}")
    @PreAuthorize(name = "店铺激活邮件", sn = "shop:Enable")
    public void Enable(@PathVariable("id")Long id){
        shopService.Enable(id);
    }


    @GetMapping("/{id}")

    public Shop loadById(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "店铺查询", sn = "shop:loadAll")
    public List<Shop> loadAll(){

        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "店铺列表", sn = "shop:queryRows")
    public PageList<Shop> queryRows(@RequestBody ShopQuery query)
    {

        return shopService.queryRows(query);
    }


    /**
     * @Title: settlement
     * @Description: 商铺入驻
     * @Author: winnie
     * @Version: 1.0
     * @Date:  1/7/2022 下午3:15
     * @Parameters: [shop]
     * @Return cn.knight.basic.utils.AjaxResult
     */
    @PostMapping("/settlement")
    @PreAuthorize(name = "店铺入驻", sn = "shop:settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (RuntimeException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
    * @Title: reject
    * @Description: 入驻驳回
    * @Author: winnie
    * @Version: 1.0
    * @Date:  2/7/2022 下午8:41
    * @Parameters: [shopAuditLog]
    * @Return cn.knight.basic.utils.AjaxResult
    */
    @PostMapping("/audit/reject")
    @PreAuthorize(name = "店铺入驻驳回", sn = "shop:reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
    * @Title: pass
    * @Description:  入驻审核通过
    * @Author: winnie
    * @Version: 1.0
    * @Date:  2/7/2022 下午8:41
    * @Parameters: [shopAuditLog]
    * @Return cn.knight.basic.utils.AjaxResult
    */
    @PostMapping("/audit/pass")
    @PreAuthorize(name = "店铺入驻通过", sn = "shop:pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
    * @Title: not
    * @Description: 审核拒绝
    * @Author: winnie
    * @Version: 1.0
    * @Date:  3/7/2022 上午11:00
    * @Parameters: [shopAuditLog]
    * @Return cn.knight.basic.utils.AjaxResult
    */
    @PostMapping("/audit/not")
    @PreAuthorize(name = "店铺入驻拒绝", sn = "shop:not")
    public AjaxResult not(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.not(shopAuditLog);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
    * @Title: exportExcel
    * @Description: 导出excel表格
    * @Author: winnie
    * @Version: 1.0
    * @Date:  2/7/2022 下午8:42
    * @Parameters: [response]
    * @Return void
    */
    @GetMapping("/export/excel")
    @PreAuthorize(name = "店铺excel表格导出", sn = "shop:exportExcel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.loadAll();
        ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "店铺信息表.xlsx",response);

    }

    /**
    * @Title: importExcel
    * @Description: 导入excel
    * @Author: winnie
    * @Version: 1.0
    * @Date:  2/7/2022 下午9:07
    * @Parameters: [file]
    * @Return void
    */
    @PostMapping("/import/excel")
    @PreAuthorize(name = "店铺excel表格导入", sn = "shop:importExcel")
    public void importExcel(@RequestPart(value = "file", required = true) MultipartFile file){
        // 1.使用EasyPoi帮你解析Excel，解析成Java对象
        // MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);

        shopService.patchsave(shops);

        shops.forEach(System.out::println);
    }

    /**
    * @Title: echarts
    * @Description: 后台首页柱状图展示
    * @Author: winnie
    * @Version: 1.0
    * @Date:  2/7/2022 下午9:07
    * @Parameters: []
    * @Return java.util.Map<java.lang.String,java.lang.Object>
    */
    @GetMapping("/echarts")
    public Map<String, Object> echarts(){
        return shopService.loadEcharts();
    }
}
